<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item to="/">首页</el-breadcrumb-item>
      <el-breadcrumb-item to="/home/product/spumanage"
        >商品管理</el-breadcrumb-item
      >
      <el-breadcrumb-item to="/home/product/attributetemplatemanage"
        >属性模板管理</el-breadcrumb-item
      >
      <el-breadcrumb-item>属性列表管理</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <div class="box">
      <el-row>
        <el-col :span="8">
          <div class="grid-content bg-purple">
            <h3 style="line-height: 32px">
              <i class="el-icon-s-tools"></i> &nbsp; 管理信息
            </h3>
          </div>
        </el-col>
        <el-col :span="16" style="text-align: right">
          <div class="grid-content bg-purple">
            <el-button
              size="small"
              type="primary"
              icon="el-icon-circle-plus-outline"
              @click="
                $router.push(
                  `/home/product/addsalesattribute/${attrTempId}`
                )
              "
              >添加属性</el-button
            >
            <el-button size="small" type="primary" icon="el-icon-remove-outline"
              >删除属性</el-button
            >
          </div>
        </el-col>
      </el-row>
      <div class="divider"></div>

      <el-row :gutter="15" class="whererow">
        <el-col :span="6" style="line-height: 32px"
          >当前模板（<b>{{ template.name }}</b
          >）的销售属性共{{ total }}个。</el-col
        >
        <el-col :span="6">
          <el-input
            v-model="search"
            type="text"
            size="small"
            placeholder="请输入属性名称"
          >
            <i slot="prefix" class="el-input__icon el-icon-search"></i>
          </el-input>
        </el-col>
      </el-row>
    </div>

    <div class="noticerow box">
      <i class="el-icon-document"></i> &nbsp; 销售属性列表
    </div>

    <el-table
      border
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
    >
      <el-table-column align="center" label="编号" width="60" type="index">
      </el-table-column>
      <el-table-column align="center" label="属性名称">
        <template slot-scope="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column align="center" label="属性值录入方式" width="350">
        <template slot-scope="scope">
          <span v-if="scope.row.inputType == 0">手动输入</span>
          <span v-if="scope.row.inputType == 1">提供备选值列表(单选录入)</span>
          <span v-if="scope.row.inputType == 2">提供备选值列表(多选录入)</span>
          <span v-if="scope.row.inputType == 3"
            >提供备选值列表(下拉框单选录入)</span
          >
          <span v-if="scope.row.inputType == 4"
            >提供备选值列表(下拉框多选录入)</span
          >
        </template>
      </el-table-column>
      <el-table-column align="center" label="可选值内容">
        <template slot-scope="scope">{{ scope.row.valueList }}</template>
      </el-table-column>
      <el-table-column align="center" label="排序" width="100">
        <template slot-scope="scope">{{ scope.row.sort }}</template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="170">
        <template slot-scope="scope">
          <el-button
            title="编辑属性"
            type="warning"
            size="mini"
            icon="el-icon-edit"
            circle
            @click="
              $router.push('/home/product/updatesalesattribute/' + scope.row.id)
            "
          ></el-button>
          <el-button
            title="删除属性"
            type="danger"
            size="mini"
            icon="el-icon-delete"
            circle
            @click="deleteAttribute(scope.row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination">
      <el-pagination
        background
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        layout="total, prev, pager, next, jumper"
        :total="total"
        :page-size="pageSize"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      template: {},                 // 保存当前模板信息
      attrTempId: 0,                // 保存当前操作的属性模板ID
      currentPage: 1,
      total: 0,
      pageSize: 5,

      search: "",                   // 绑定搜索文本框的值
      tableData: [],                // 绑定表格数据
    };
  },
  methods: {
    /**
     *  删除所选中的属性模板 
     */
    deleteAttribute(id) {
      // 弹出确认对话框
      this.$confirm("确认删除此属性？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$api.attributeApi.delete({ id }).then((res) => {
          console.log("删除选中的属性：", res);
          if (res.data.state == 200) {
            this.$message({
              showClose: true,
              message: "删除属性成功",
              type: "success",
            });
            // 刷新属性模板列表
            this.loadAttributes();
          }else{
            this.$alert(res.data.message, '提示')
          }
        });
      });
    },

    /**
     *  处理更新页码后的业务 
     */
    handleCurrentChange(val) {
      console.log(`即将加载选中页: 第${val}页`);
      this.currentPage = val;
      this.loadAttributes();
    },

    /**
     *  加载当前模板下的属性列表 
     */
    loadAttributes() {
      this.$api.attributeApi
        .querySalesByTemplateId({
          templateId: this.attrTempId,
          pageSize: this.pageSize,
        })
        .then((res) => {
          console.log("加载当前模板下的属性列表：", res);
          if(res.data.state==200){
            this.tableData = res.data.data.list;
            this.total = res.data.data.total;
          }else{
            this.$alert(res.data.message, '提示')
          }
        });
    },

    /**
     *  加载当前模版 
     */
    loadTemplate() {
      this.$api.attrTemplateApi
        .queryById({ id: this.attrTempId })
        .then((res) => {
          console.log("加载当前模板", res);
          this.template = res.data.data;
        });
    },
  },

  /**
   *  页面挂载完毕后执行
   */
  mounted() {
    this.attrTempId = this.$route.params.attributeTemplateId; // 存储属性模板ID
    // 加载当前模板对象
    this.loadTemplate();
    // 加载当前模板下的属性列表
    this.loadAttributes();
  },
};
</script>
